package com.project.restaurant.thirdpart.controller;

import com.project.restaurant.common.api.ResponseResult;
import com.project.restaurant.common.api.ServiceReturn;
import com.project.restaurant.common.api.ServiceStateCode;
import com.project.restaurant.thirdpart.dto.LookPayResult;
import com.project.restaurant.thirdpart.dto.WXNativePayOrder;
import com.project.restaurant.thirdpart.service.WXNativePayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/wx/nativePay")
public class WXNativePayController {

    @Autowired
    WXNativePayService wxNativePayService;

    @PostMapping(value = "/generate/payUrl",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<String> generateNativePayUrl(@RequestBody @Validated WXNativePayOrder order){
        ServiceReturn<String> result = null;
        try {
            result = wxNativePayService.createNativePayUrl(order);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.failure(ServiceStateCode.WXPAY_FAILURE);
        }
        return ResponseResult.auto(result.getStateCode(),result.getReturnData());
    }

    @GetMapping(value = "/lookOrder/payState/{orderId}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<LookPayResult> lookOrderPayState(@PathVariable("orderId") String orderId){
        ServiceReturn<LookPayResult> result = null;
        try {
            result = wxNativePayService.isPaySuccess(orderId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.failure(ServiceStateCode.WXPAY_FAILURE);
        }
        return ResponseResult.auto(result.getStateCode(),result.getReturnData());
    }

}
